package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecordArchives;
import com.ruoyi.system.domain.RecordArchivesBook;
import com.ruoyi.system.domain.vo.BarChartVo;
import com.ruoyi.system.service.IRecordArchivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 案卷管理Controller
 *
 * @author admin
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/system/archives")
public class
RecordArchivesController extends BaseController {
    @Autowired
    private IRecordArchivesService recordArchivesService;

    /**
     * 查询案卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:archives:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordArchives recordArchives) {
        startPage();
        List<RecordArchives> list = recordArchivesService.selectRecordArchivesList(recordArchives);
        return getDataTable(list);
    }

    /**
     * 查询案卷管理列表
     */
    @GetMapping("/listByName")
    public TableDataInfo listArchivesByName(RecordArchives archives) {
        List<RecordArchives> list = recordArchivesService.selectRecordArchivesListByName(archives);
        return getDataTable(list);
    }

    /**
     * 查询案卷信息台账
     */
    @PreAuthorize("@ss.hasPermi('system:archives:listBook')")
    @GetMapping("/listBook")
    public TableDataInfo listBook(RecordArchivesBook recordArchivesBook) {
        startPage();
        List<RecordArchivesBook> list = recordArchivesService.selectRecordArchivesListBook(recordArchivesBook);
        return getDataTable(list);
    }

    /**
     * 导出案卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:archives:export')")
    @Log(title = "案卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordArchives recordArchives) {
        List<RecordArchives> list = recordArchivesService.selectRecordArchivesList(recordArchives);
        ExcelUtil<RecordArchives> util = new ExcelUtil<RecordArchives>(RecordArchives.class);
        util.exportExcel(response, list, "案卷管理数据");
    }

    /**
     * 导出案卷台账
     */
    @PreAuthorize("@ss.hasPermi('system:archives:exportBook')")
    @Log(title = "案卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportBook")
    public void exportBook(HttpServletResponse response, RecordArchivesBook recordArchivesBook) {
        List<RecordArchivesBook> list = recordArchivesService.selectRecordArchivesListBook(recordArchivesBook);
        ExcelUtil<RecordArchivesBook> util = new ExcelUtil<RecordArchivesBook>(RecordArchivesBook.class);
        util.exportExcel(response, list, "案卷台账数据");
    }

    /**
     * 获取案卷管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:archives:query')")
    @GetMapping(value = "/{archivesId}")
    public AjaxResult getInfo(@PathVariable("archivesId") String archivesId) {
        return AjaxResult.success(recordArchivesService.selectRecordArchivesByArchivesId(archivesId));
    }

    /**
     * 新增案卷管理
     */
    @PreAuthorize("@ss.hasPermi('system:archives:add')")
    @Log(title = "案卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordArchives recordArchives) {
        return toAjax(recordArchivesService.insertRecordArchives(recordArchives));
    }

    /**
     * 修改案卷管理
     */
    @PreAuthorize("@ss.hasPermi('system:archives:edit')")
    @Log(title = "案卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordArchives recordArchives) {
        return toAjax(recordArchivesService.updateRecordArchives(recordArchives));
    }

    /**
     * 删除案卷管理
     */
    @PreAuthorize("@ss.hasPermi('system:archives:remove')")
    @Log(title = "案卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{archivesIds}")
    public AjaxResult remove(@PathVariable String[] archivesIds) {
        return toAjax(recordArchivesService.deleteRecordArchivesByArchivesIds(archivesIds));
    }

    /**
     * 案卷归档申请
     */
    @Log(title = "案卷归档申请", businessType = BusinessType.UPDATE)
    @PutMapping("/apply/{archivesIds}")
    public AjaxResult updateArchivesApply(@PathVariable String[] archivesIds) {
        return toAjax(recordArchivesService.updateArchivesApplyByIds(archivesIds));
    }

    /**
     * 案卷归档通过
     */
    @Log(title = "案卷归档通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{archivesIds}")
    public AjaxResult updateArchivesApprove(@PathVariable String[] archivesIds) {
        return toAjax(recordArchivesService.updateArchivesApprove(archivesIds));
    }

    /**
     * 案卷归档拒绝
     */
    @Log(title = "案卷归档拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{archivesIds}")
    public AjaxResult updateArchivesReject(@PathVariable String[] archivesIds) {
        return toAjax(recordArchivesService.updateArchivesReject(archivesIds));
    }

    /**
     * 案卷导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecordArchives> util = new ExcelUtil<RecordArchives>(RecordArchives.class);
        util.importTemplateExcel(response, "案卷数据");
    }

    /**
     * 案卷导入
     */
    @Log(title = "案卷导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:archives:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecordArchives> util = new ExcelUtil<RecordArchives>(RecordArchives.class);
        List<RecordArchives> archivesList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = recordArchivesService.importArchives(archivesList, operName);
        return AjaxResult.success(message);
    }

    // 柱状图，饼状图数据
    @GetMapping("/bar")
    public TableDataInfo bar(BarChartVo barChartVo) {
        List<BarChartVo> list = recordArchivesService.selectBarChartVoList(barChartVo);
        return getDataTable(list);
    }
}
